﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema
{
    public partial class QuanLyDoiTuongXem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydsDTX();
        }

        public void LaydsDTX()
        {
            DataTable ds_dtx = Timkiem_MA("");
 
            for (int i = 0; i < ds_dtx.Rows.Count; i++)
            {
                string str = ds_dtx.Rows[i]["MaDT"].ToString();
                DDLMaDTX.Items.Add(str);
            }
            grDoiTuongXem.DataSource = ds_dtx;
            grDoiTuongXem.DataBind();
        }

        public DataTable Timkiem_Ten(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@tendt", ten));
                   
                    return CDatastore.DocStore("SP_TIMKIEM_DOITUONGXEM_TENDT", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_DOITUONGXEM_TENDT");
        }
        public DataTable Timkiem_MA(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAdt", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_DOITUONGXEM_MADT", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_DOITUONGXEM_MADT");
        }
        protected void DDLMaDTX_SelectedIndexChanged(object sender, EventArgs e)
        {
       
            DataTable dt = Timkiem_MA(DDLMaDTX.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                txtTenDTX.Text = dt.Rows[0][1].ToString();
            }
        }

        protected void grDoiTuongXem_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLMaDTX.Text = grDoiTuongXem.SelectedRow.Cells[1].Text;
            txtTenDTX.Text = grDoiTuongXem.SelectedRow.Cells[2].Text;
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@tendt", txtTenDTX.Text.ToString()));
                CDatastore.ThucHienStore("SP_INSERT_DOITUONGXEM", sqlPara);
                lblThongBao.Text = "Bạn đã thêm Đối Tượng Xem thành công";
                DDLMaDTX.Items.Clear();
                txtTenDTX.Text = null;
                LaydsDTX();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@madt", DDLMaDTX.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_DOITUONGXEM", sqlPara);
                lblThongBao.Text = "Bạn đã xóa Mã Đối Tượng Xem " + DDLMaDTX.Text + " thành công";
                DDLMaDTX.Items.Clear();
                LaydsDTX();
                txtTenDTX.Text = null;

            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@madt", DDLMaDTX.Text.ToString()));
                sqlPara.Add(new SqlParameter("@tendt", txtTenDTX.Text.ToString()));
                CDatastore.ThucHienStore("SP_UPDATE_DOITUONGXEM", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Đối Tượng Xem " + DDLMaDTX.Text + " thành công";
                DDLMaDTX.Items.Clear();
                LaydsDTX();
                txtTenDTX.Text = null;
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void grDoiTuongXem_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grDoiTuongXem.PageIndex = e.NewPageIndex;
            grDoiTuongXem.DataBind();
            LaydsDTX();
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            DataTable dt = Timkiem_Ten(txtTenDTX.Text);
            grDoiTuongXem.DataSource = dt;
            grDoiTuongXem.DataBind();
        }






    }
}